import cv2
import numpy as np
img = cv2.imread('cat.png')
cv2.imshow('show', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

def nearest_interpolation_v1(src, dst_size):
  src_h, src_w, channel = src.shape
  dst_w, dst_h = dst_size
  x_scale = dst_w / src_w
  y_scale = dst_h / src_h
  dst = np.zeros((dst_h, dst_w, channel), dtype=src.dtype)
  for dst_x in range(dst_w):
    for dst_y in range(dst_h):
      src_x = int(round((dst_x + 0.5) / x_scale - 0.5))
      src_y = int(round((dst_y + 0.5) / y_scale - 0.5))
      src_x = src_x if src_x > 0 else 0
      src_x = src_x if src_x < src_w else src_w - 1
      src_y = src_y if src_y > 0 else 0
      src_y = src_y if src_y < src_h else src_h - 1
      dst[dst_y, dst_x] = src[src_y, src_x]
  return dst

img_n1 = nearest_interpolation_v1(img, (512, 512))
cv2.imshow('show', img_n1)
cv2.waitKey(0)
cv2.destroyAllWindows()